package com.wtp.wallpaper.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtp.wallpaper.common.constant.MessageConstant;
import com.wtp.wallpaper.common.constant.PictureOssPathConstant;
import com.wtp.wallpaper.common.exception.ClassifyExistException;
import com.wtp.wallpaper.pojo.dto.ClassifyDTO;
import com.wtp.wallpaper.pojo.dto.PictureDTO;
import com.wtp.wallpaper.pojo.entity.Classify;
import com.wtp.wallpaper.mapper.ClassifyMapper;
import com.wtp.wallpaper.pojo.entity.Picture;
import com.wtp.wallpaper.service.ClassifyService;
import com.wtp.wallpaper.service.PictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    @Autowired
    private PictureService pictureService;

    @Override
    public String removeClassifyByIds(ClassifyDTO classifyDTO) {

        String[] removeIds = classifyDTO.getRemoveIds();
        List<String> ids = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        //sb.append("删除成功").append("\n");
        for(String id : removeIds){
            if(!pictureService.exists(new QueryWrapper<Picture>().eq("class_id",id))){
                ids.add(id);
            }else {
                sb.append("分类: ").append(id).append(" 被引用无法删除").append("\n");
            }
        }
        removeByIds(ids);
        return sb.toString();
    }

    @Override
    public void addOne(ClassifyDTO classifyDTO) {

        if(exists(new QueryWrapper<Classify>().eq("name",classifyDTO.getName()))){
            throw new ClassifyExistException(MessageConstant.CLASSIFY_EXISTS);
        }

        //缩略图位置
        String f2 = PictureOssPathConstant.PIC_SMALL_URL + "/" + classifyDTO.getFileName();
        Classify classify = Classify.builder()
                .name(classifyDTO.getName())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .picurl(f2)
                .build();
        save(classify);
        log.error("插入后生成id,{}",classify.getId());
        pictureService.addOne(PictureDTO.builder()
                .classId(String.valueOf(classify.getId()))
                .fileName(classifyDTO.getFileName())
                .fileMd5(classifyDTO.getFileMd5())
                .build());

    }

    @Override
    public void update(ClassifyDTO classifyDTO) {
        Classify classify = BeanUtil.copyProperties(classifyDTO, Classify.class);
        try{
            classify.setUpdateTime(LocalDateTime.now());
            updateById(classify);
        }catch (Exception e){
            throw new ClassifyExistException(MessageConstant.CLASSIFY_EXISTS);
        }
    }
}
